//
//  RCAdController.m
//  ReCite
//
//  Created by Miles on 10-05-14.
//  Copyright 2010 Miles Thorogood
//  greenmeat.net. All rights reserved.
//

#import "RCAdController.h"
#import "RCCommunicationController.h"
#import "RCAdModel.h"

@implementation RCAdController

@synthesize dataDictionary ;
@synthesize arrayOfAds ;
@synthesize comController ;

- (id) init {
	self = [super init] ;
	if (self) {
				
		arrayOfAds = [[NSMutableArray alloc] init] ;
		// start the communication and location
		self.comController = [[RCCommunicationController alloc] initWithDelegate:self] ;
		
	}
	return self ;
}

-(void) dealloc {
	[dataDictionary release] ;
	[arrayOfAds release] ;
	[comController release] ; 
	[super dealloc] ;	
}

// from RCCommunicationController
- (void) locationDataReceived:(id) handler {
	self.dataDictionary = [handler mediaDictionary];
	[handler release];

	NSString *type = (NSString *) [dataDictionary objectForKey:@"type"] ; // check if good
	
	if (![type isEqualToString:@""]) {
		printf("Got an ad");
		BOOL flag = NO ;
		for(RCAdModel *ad in arrayOfAds) {
			if([ad.thisAd.ad_identifier isEqualToString:[dataDictionary objectForKey:@"ad"]]) 
				flag = YES ;
		}
		if (!flag) {
			printf("Got a new ad");
			RCAdModel *mAnn = [[RCAdModel alloc] initWithDictionary:dataDictionary];
			[arrayOfAds addObject:mAnn] ;
		}
		
	}
}


@end
